home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / creator changer / creator changer project / source / buffereddrawer.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  3.1 KB  |  107 lines

  1. import java.awt.Container;
  2. import java.awt.Image;
  3. import java.awt.Graphics;
  4. import java.awt.Dimension;
  5.  
  6. /**
  7.  * A component which does all it's drawing in an offscreen image
  8.  * the size of the component.
  9.  *
  10.  * @author Levi Brown
  11.  * @author Apple Worldwide Developer Technical Support
  12.  * @author Apple Computer Inc.
  13.  * @version 1.0 11/4/1998
  14.  */
  15. public class BufferedDrawer extends Container
  16. {
  17.     /**
  18.      * Creates a new BufferedDrawer.
  19.      */
  20.     public BufferedDrawer()
  21.     {
  22.         //Initialize our data members
  23.         workingGraphics     = null;
  24.         workingImage        = null;
  25.     }
  26.  
  27.     /**
  28.      * Handles drawing the desired content into the offscreen buffer.
  29.      * Override this to do your own drawing (be sure to call super.draw()).
  30.      * @see #paint
  31.      */
  32.     protected void draw()
  33.     {
  34.         //Make sure the offscreen image is ready to be drawn into.
  35.         ensureValidWorkingImage();
  36.  
  37.         //Do drawing here (use workingGraphics)
  38.         //Don't forget to erase the buffer if you don't draw over the entire area
  39.         //or else you will end up with tracers.
  40.         //To erase the entire area, uncomment out the following code:
  41.         /*
  42.         Dimension s = new Dimension(workingImage.getWidth(this), workingImage.getHeight(this));
  43.         workingGraphics.setColor(getBackground());
  44.         workingGraphics.fillRect(0, 0, s.width, s.height);
  45.         */
  46.     }
  47.  
  48.     /**
  49.      * Makes sure the offscreen image exists.
  50.      */
  51.     protected void ensureValidWorkingImage()
  52.     {
  53.         //If the size changed and we need to create a new pane image, then create one
  54.         Dimension size = getSize();
  55.         if ((size != null && size.width > 0 && size.height > 0) && (workingImage == null || size.width != workingImage.getWidth(this) || size.height != workingImage.getHeight(this)))
  56.         {
  57.             if (workingImage != null)
  58.             {
  59.                 workingImage.flush();
  60.                 workingImage = null;
  61.             }
  62.             workingImage = createImage(size.width, size.height);
  63.             if (workingGraphics != null)
  64.             {
  65.                 workingGraphics.dispose();
  66.                 workingGraphics = null;
  67.             }
  68.             workingGraphics = workingImage == null ? null : workingImage.getGraphics();
  69.         }
  70.     }
  71.  
  72.     /** 
  73.      * Updates the component. This method is called in
  74.      * response to a call to repaint. You can assume that
  75.      * the background is not cleared.
  76.      * Overriden here to prevent unwanted background erasing.
  77.      * @param g the specified Graphics window
  78.      * @see #paint
  79.      * @see java.awt.Component#repaint
  80.      */
  81.     public void update(Graphics g)
  82.     {
  83.         paint(g);
  84.     }
  85.  
  86.     /** 
  87.      * Paints the component.  This method is called when the contents
  88.      * of the component should be painted in response to the component
  89.      * first being shown or damage needing repair.  The clip rectangle
  90.      * in the Graphics parameter will be set to the area which needs
  91.      * to be painted.
  92.      * @param g the specified Graphics object
  93.      * @see #update
  94.      */
  95.     public void paint(Graphics g)
  96.     {
  97.         draw();
  98.         g.drawImage(workingImage, 0, 0, this);
  99.         super.paint(g);
  100.     }
  101.  
  102.     //The offscreen image buffer to render the progress bar into.
  103.     protected Image workingImage;
  104.     //The Graphics object associated with the offscreen image buffer.
  105.     protected Graphics workingGraphics;
  106. }
  107.